package com.yc.UI;

import java.util.HashMap;
import java.util.Map;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseMoveListener;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.wb.swt.SWTResourceManager;

import com.yc.dao.UserDao;
import com.yc.utils.RegisterUtil;
import com.yc.utils.SWTUtil;
import com.yc.utils.StringUtil;
import com.yc.utils.Values;

import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.FocusAdapter;
import org.eclipse.swt.events.FocusEvent;

/**
 *用户登录界面
 * @author UNO
 * 2021年6月20日下午3:42:45
 */
public class UserLoginUI {

	protected Shell shell;
	private boolean down = false;
	private int clickX = 0;
	private int clickY = 0;
	private Text txtA;
	private CLabel Label1;
	private CLabel Label2;

	private RegisterUtil rsu = new RegisterUtil();

	/**
	 * Launch the application.
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			UserLoginUI window = new UserLoginUI();
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();
		shell.open();
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		shell = new Shell(SWT.NONE);
		shell.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		shell.setImage(SWTResourceManager.getImage(WelcomeUI.class, "/images/福字.png"));	
		shell.setSize(905, 507);
		shell.setText("SWT Application");
		shell.setLayout(new FillLayout(SWT.HORIZONTAL));

		Composite composite = new Composite(shell, SWT.NONE);
		composite.setBackgroundMode(SWT.INHERIT_FORCE);
		//背景图片
		composite.setBackgroundImage(SWTResourceManager.getImage(WelcomeUI.class, "/images/back.jpg"));
		// 设置窗口居中
		SWTUtil.centerWindom(shell);


		//设置图标
		Label label_Icon = new Label(composite, SWT.NONE);
		label_Icon.setBounds(0, 0, 40, 40);
		label_Icon.setImage(SWTResourceManager.getImage(WelcomeUI.class,"/images/福字.png"));

		//关闭按钮
		Label label_close = new Label(composite, SWT.NONE);

		label_close.setBounds(863, 0, 40, 40);
		label_close.setImage(SWTResourceManager.getImage(WelcomeUI.class,"/images/关闭.png"));

		//最小化按钮
		Label label_min = new Label(composite, SWT.NONE);
		label_min.setBounds(817, 0, 40, 40);
		label_min.setImage(SWTResourceManager.getImage(WelcomeUI.class,"/images/最小化.png"));

		Combo combo_account = new Combo(composite, SWT.NONE);
		combo_account.setBounds(501, 157, 92, 28);
		combo_account.setItems(new String[] {"navy", "张三", "李四", "王五", "yc", "nh"});

		Label Label_account = new Label(composite, SWT.CENTER);
		Label_account.setFont(SWTResourceManager.getFont("Papyrus", 14, SWT.NORMAL));
		Label_account.setBounds(284, 151, 92, 40);
		Label_account.setText("账号");

		Label label_pwd = new Label(composite, SWT.CENTER);
		label_pwd.setFont(SWTResourceManager.getFont("Papyrus", 14, SWT.NORMAL));
		label_pwd.setBounds(302, 245, 60, 40);
		label_pwd.setText("密码");

		txtA = new Text(composite, SWT.BORDER | SWT.PASSWORD);
		txtA.setBounds(500, 251, 129, 26);

		Label Label_title = new Label(composite, SWT.CENTER);
		Label_title.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 10, SWT.NORMAL));
		Label_title.setBounds(46, 10, 85, 30);
		Label_title.setText("用户登录");

		Button btnCheckButton = new Button(composite, SWT.CHECK | SWT.CENTER);
		btnCheckButton.setBounds(302, 346, 121, 20);
		btnCheckButton.setText("记住密码");

		Button button_login = new Button(composite, SWT.CENTER);
		button_login.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 14, SWT.NORMAL));
		button_login.setBackground(SWTResourceManager.getColor(SWT.COLOR_TRANSPARENT));
		button_login.setBounds(396, 441, 119, 40);
		button_login.setText("登录");

		Label Label_return = new Label(composite, SWT.CENTER);

		Label_return.setBounds(23, 46, 40, 40);
		Label_return.setImage(SWTResourceManager.getImage(WelcomeUI.class,"/images/return.png"));

		Label1 = new CLabel(composite, SWT.NONE);
		Label1.setBounds(501, 191, 199, 20);
		Label1.setText("请输入账号...");

		Label2 = new CLabel(composite, SWT.NONE);
		Label2.setText("请输入密码...");
		Label2.setBounds(501, 283, 199, 20);

		//关闭按钮操作
		label_close.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				shell.dispose();
				System.exit(0);
			}
		});
		//最小化操作
		label_min.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				shell.setMinimized(true);
			}
		});
		//面板拖动
		composite.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDown(MouseEvent e) {
				down = true;
				clickX = e.x;
				clickY = e.y;
			}
			@Override
			public void mouseUp(MouseEvent e) {
				down = false;
			}
		});

		composite.addMouseMoveListener(new MouseMoveListener() {
			public void mouseMove(MouseEvent e) {
				if(down) {  //down为true，说明鼠标按下，窗口跟着鼠标移动			
					shell.setLocation(shell.getLocation().x + e.x - clickX, shell.getLocation().y + e.y - clickY);
				}

			}
		});
		//返回
		Label_return.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				WelcomeUI wc = new WelcomeUI();
				shell.dispose();
				wc.open();
			}
		});

		//账号框回车向下转移焦点
		combo_account.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				Label1.setForeground(SWTResourceManager.getColor(SWT.COLOR_BLUE));
				Label1.setText("请输入账号..");
				if(e.keyCode == 13) {  //说明用户按下了回车键
					txtA.setFocus();
				}

			}
		});
		//账号框失焦事件
		combo_account.addFocusListener(new FocusAdapter() {
			@Override
			public void focusLost(FocusEvent e) {
				String str = combo_account.getText();
				if(StringUtil.checkNull(str)) {
					Label1.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));
					Label1.setText("账号不能为空");
					combo_account.setFocus();
					return;
				}
				Label1.setForeground(SWTResourceManager.getColor(SWT.COLOR_DARK_GREEN));
				Label1.setText("账号格式正确");

			}
		});
		Event event = new Event();
		event.widget = button_login;
		//密码框键盘回车事件
		txtA.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				Label2.setForeground(SWTResourceManager.getColor(SWT.COLOR_BLUE));
				Label2.setText("请输入密码...");
				if(e.keyCode ==13) { //说明用户按下了回车键
					String str = txtA.getText();
					if(StringUtil.checkNull(str)) {
						Label2.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));
						Label2.setText("密码不能为空");
						txtA.setFocus();
						return;
					}
					button_login.notifyListeners(SWT.MouseUp, event);
				}
			}
		});

		//点击登陆
		button_login.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				String account = combo_account.getText().trim();
				String pwd = txtA.getText().trim();
				//判空
				if(StringUtil.checkNull(account)) {
					Label1.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));
					Label1.setText("账号不能为空");
					combo_account.setFocus();
					return;
				}

				UserDao userdao = new UserDao();
				Map<String, Object> map = userdao.findUserInfo(account, pwd);
				if(map == null || map.isEmpty()) { //说明没有取到值，登录失败
					SWTUtil.showMessage(shell, "错误提示", "账号或者密码有误，请重新输入...");
					return;
				}
				int i = Integer.valueOf(map.get("u_status").toString());
				if(i == 1) {
					SWTUtil.showMessage(shell, "错误提示", "账号已被封禁，如有异议，请联系管理员...");
					return;
				}

				//记住密码
				if(btnCheckButton.getSelection()){						
					if(StringUtil.checkNull(account,pwd)) {
						SWTUtil.showMessage(shell, "错误提示","信息不完整，记不住");
					}
					Map<String,String> loginmap = new HashMap<String, String>();
					loginmap.put("account", map.get("u_name").toString());
					loginmap.put("pwd", map.get("u_pwd").toString());
					rsu.addRegister(loginmap);
				}else {
					rsu.deleteInfo("U_pwd");
				}

				//如果取到值，说明登录成功  map里存的是该用户所有的信息
				Values.currentLoginUser = map;
				MainUI m = new MainUI();			
				shell.dispose();
				m.open();
			}
		});

		//默认读取注册表的内容 设置到对应文本框中  记住密码  account 和 pwd 是注册表里面保存的的账号和密码
		String name = rsu.findInfo("account");
		String pwd = rsu.findInfo("pwd");
		//判断操作
		if( !StringUtil.checkNull(name)) {
			combo_account.setText(name);
		}
		if( !StringUtil.checkNull(pwd)) {
			txtA.setText(pwd);
		}
	}
}
